﻿using Common.Prism.Apps;
using Common.Prism.Events;
using NxChatQQ.Models;
using NxChatQQ.Models.Events;
using NxChatQQ.Views.Layouts;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using System.Windows.Input;

namespace NxChatQQ.ViewModels.Pages
{
    public class MainPageViewModel : BaseViewModel
    {


        public ICommand CreateGroupCommand { get; set; }
        public ICommand AddFriendCommand { get; set; }
        public IRegionManager RegionManager { get; private set; }

        public MainPageViewModel(IEventAggregator _eventAggregator, IRegionManager _regionManager)
        {

            CreateGroupCommand = new DelegateCommand(CreateGroupExecuted);
            AddFriendCommand = new DelegateCommand(QueryFriendExecuted);

            RegionManager = _regionManager;
            _regionManager.RegisterViewWithRegion("ChatListRegion", "RecentListLayout");
            _regionManager.RegisterViewWithRegion("ChatPageRegion", typeof(ChatMessageLayout));


            // 订阅事件
            _eventAggregator.GetEvent<UpdateNavEvent>().Subscribe(UpdateNavMethod);
            _eventAggregator.GetEvent<UpdateChatEvent>().Subscribe(UpdateChatMethod);
            _eventAggregator.GetEvent<UpdateFriendInfoEvent>().Subscribe(UpdateFriendInfo);
        }

        private void QueryFriendExecuted()
        {
            var dialog = new DialogData();
            dialog.ViewName = "QueryFriendView";
            dialog.AllowClose = false;
            ShowDialog(dialog);
        }


        private void CreateGroupExecuted()
        {
            var dialog = new DialogData();
            dialog.ViewName = "CreateGroupView";
            dialog.AllowClose = false;
            ShowDialog(dialog);
        }

        private void UpdateNavMethod(int index)
        {
            switch (index)
            {
                case 0:
                    RegionManager.RequestNavigate("ChatPageRegion", "RecentListLayout");
                    break;
                case 1:
                    RegionManager.RequestNavigate("ChatPageRegion", "FriendListLayout");
                    break;
                default:
                    break;
            }
        }


        private void UpdateFriendInfo(Friend friend)
        {
            //throw new NotImplementedException();
        }


        private void UpdateChatMethod(Friend friend)
        {
            //_regionManager.RequestNavigate("ChatPageRegion", "ChatMessageLayout");
        }
    }
}
